package com.shanghaionstar.user.presenter;

import android.os.Bundle;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.CategoryType;
import com.onstar.cn.more.entity.ContentHeaderType;
import com.onstar.cn.more.response.GetAllContentListResponseType;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.DetailContentWebHandler;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.user.iview.IGuideView;
import com.shanghaionstar.user.service.GuideService;

import java.util.HashMap;
import java.util.List;

import rx.functions.Action1;

/**
 * Created by 7828wc on 2016/6/29.
 */
public class GuidePresenter extends BasePresenter<GetAllContentListResponseType> {
    private HashMap<String, GetAllContentListResponseType> contentList;
    private BaseActivity mContext;
    private CategoryType categoryType;
    private CategoryType[] categoryTypes = { CategoryType.TRUE_STORY, CategoryType.ASSISTANT,
            CategoryType.ONSTAR_FWJS };
//    private int currentPos = 0;
    private GuideService service;
    private IGuideView mGuideView;

    public GuidePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<GetAllContentListResponseType> view) {
        super.setView(view);
        mContext = (BaseActivity) view.onActivity();
        mGuideView = (IGuideView) view;
        service = new GuideService(mContext);
    }

    public void initData(Bundle bun){
        contentList = new HashMap<String, GetAllContentListResponseType>();
        if(bun != null ){
            int currentPos = bun.getInt("categoryType");
            categoryType = categoryTypes[currentPos];
            if(currentPos != 0) {
                mGuideView.performClick(currentPos);
            }else {
                categoryType = categoryTypes[0];
                getContentList();
            }
        }else{
            categoryType = categoryTypes[0];
            getContentList();
        }
    }

    public void getContentList(){
        mGuideView.setLoading(true);
        subscription=service.getContentList(categoryType).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<GetAllContentListResponseType>() {
                    @Override
                    public void call(GetAllContentListResponseType contentListResponse) {
                        mGuideView.setLoading(false);
                        contentList.put(categoryType.value(), contentListResponse);
                        initContentListPage();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mGuideView.setLoading(false);
                        if(throwable != null)
                            mGuideView.onResponseError((OnStarException) throwable);
                    }
                });
    }

    public void onTitleClick(int pos){
        categoryType = categoryTypes[pos];
        if(contentList.get(categoryType.value()) != null){
            initContentListPage();
        }else{
            getContentList();
        }
    }

    public void onItemClick(int pos){
        try {
            String detailId = contentList.get(categoryType.value()).getContentHeaderList().get(pos).getContentNUM();
//            GuideDetailFragment fragment = new GuideDetailFragment();

//            Intent it = new Intent(mContext, CommonWebViewActivity.class);
            Bundle bun = new Bundle();
            WebDataEntity entity = new WebDataEntity();
            entity.setClzName(DetailContentWebHandler.class.getName());
            entity.what = IWebType.TYPE_CONTENT_ID;
            bun.putString("detailId", detailId);
            bun.putSerializable("categoryType",categoryType);
            bun.putParcelable(IWebType.KEY_WEB_DATA,entity);
            CommonWebViewActivity.launchCommonWeb(mContext,bun,entity);
//            it.putExtras(bun);
//            mContext.startActivity(it);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getNoData(String[] mTitles){
        String noData = mContext.getString(R.string.option_guide_no_data);
        int N = categoryTypes.length;
        for (int i = 0; i < N; i++) {
            if (categoryTypes[i].value().equalsIgnoreCase(categoryType.value()) && mTitles != null) {
                noData = String.format(noData, mTitles[i]);
            }
        }
        return  noData;
    }


    private void initContentListPage() {
        List<ContentHeaderType> headerTypes = contentList.get(categoryType.value()).getContentHeaderList();
        if (headerTypes != null && !headerTypes.isEmpty()) {
//            List<ContentHeaderType> list = contentList.get(categoryType.value()).getContentHeaderList();
           mGuideView.showList(headerTypes);
        }else{
           mGuideView.showNoData();
        }
    }

}
